package com.kandinfo.base.admin.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.kandinfo.base.admin.constant.CacheConstant;
import com.kandinfo.base.admin.domain.DictData;
import com.kandinfo.base.admin.enumeration.DictType;
import com.kandinfo.base.admin.repository.DictDataRepository;
import com.kandinfo.base.admin.service.DictDataService;
import com.kandinfo.base.common.service.AbstractService;

/**
 * 字典
 * 
 * @author wenchongyang
 *
 */
@Service
public class DictDataServiceImpl extends AbstractService<DictData, Long, DictDataRepository>
        implements DictDataService {

    private static final String DICT_KEY = "_DICT";


    @Resource
    public void setDictDataRepository(DictDataRepository dictDataRepository) {
        super.setRepository(dictDataRepository);
    }

    @Override
    @Cacheable(value = CacheConstant.DICT_CACHE, key = "'DICT_' + #type")
    public List<DictData> getValueListByType(DictType type) {
        return super.repository.getByRef(type.name());
    }

    @Override
    public Map<String, List<DictData>> getDict(DictType... types) {
        Map<String, List<DictData>> map = new HashMap<String, List<DictData>>();
        for (DictType type : types) {
            map.put(type.name().toUpperCase() + DICT_KEY, getValueListByType(type));
        }
        return map;
    }

    @Override
    @Cacheable(value = CacheConstant.DICT_CACHE, key = "'DICT_MAP_' + #type")
    public Map<String, DictData> getValueMapByType(DictType type) {
        List<DictData> list = super.repository.getByRef(type.name());
        Map<String, DictData> map = new HashMap<String, DictData>();
        for (DictData dictData : list) {
            map.put(dictData.getCode(), dictData);
        }
        return map;
    }

    @Override
    public String findStartdate() {
        Date now = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(now);
        rightNow.add(Calendar.WEEK_OF_YEAR, -1);
        now = rightNow.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowsString = format.format(now);
        return nowsString;
    }

    @Override
    public String findEnddate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = format.format(new Date());
        return now;
    }

    @Override
    public DictData getByCode(DictType type, Object code) {
        DictData dict = getValueMapByType(type).get(String.valueOf(code));
        if (dict == null) {
            LOGGER.warn("dict {} not exsit in type {}", code, type.name());
            return new DictData();
        } else {
            return dict;
        }
    }

    @Override
    public List<DictData> getByDefCode(String type) {
        return super.repository.getByRef(type);
    }
}
